// pages/login/login.js
import { login, send } from '../../api/user'
import { setToken,setRefreshToken } from '../../utils/auth'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj: {
      mobile: '',
      code: '',
    },
    toast: '',
    isError: false,
    showBtn: true,
    timer: '60',
    countDownNum: '发送验证码',
    isCode: false,
    codeToast: ''
  },
  onClickLeft () {
    wx.switchTab({
      url: '/pages/profileuser/profileuser',
    })
  },
  inputMobile(){
    this.setData({
      isError: false,
      toast: ''
    })
  },
  inputCode(){
    this.setData({
      isCode: false,
      codeToast: ''
    })
  },
  // 手机号验证
  getMobileRight () {
    const rules = /^(?:(?:\+|00)86)?1\d{10}$/
    if (rules.test(this.data.obj.mobile)) {
      this.setData({
        isError: false,
        toast: '',
      })
      return true
    } else {
      this.setData({
        toast: '手机号格式错误',
        isError: true,
        'obj.mobile': ''
      })
      return false
    }

  },
  getCodeRight () {
    const rules = /^\d{6}$/
    if (rules.test(this.data.obj.code)) {
      this.setData({
        isCode: false,
        codeToast: '',
      })
      return true
    } else {
      this.setData({
        codeToast: '验证码格式错误',
        isCode: true,
        'obj.code': ''
      })
      return false
    }
  },
  //收集手机号
  collectMobile (e) {
    this.setData({
      'obj.mobile': e.detail.value
    }),
      this.getMobileRight()
  },
  // 收集验证码 
  collectCode (e) {
    if(!this.getMobileRight ()) return
    this.setData({
      'obj.code': e.detail.value
    }),
      this.getCodeRight()
  },
  // 发送验证
  async sendYan () {
    if (this.getMobileRight()) {
      const that = this;
      let countDownNum = '60'
      that.setData({
        timer: setInterval(function () {
          countDownNum--;
          that.setData({
            countDownNum: countDownNum + 's'
          });
          if (countDownNum == 0) {
            clearInterval(that.data.timer);
            that.setData({
              countDownNum: '发送验证码'
            });
          }
        }, 1000)
      })
      const a = await send(this.data.obj)
      console.log(a);
    }
  },
  //登录按钮
  async LoginOut() {
    if (this.getMobileRight() || this.getCodeRight()) {
      const res = await login(this.data.obj)
      console.log(res);
      wx.showToast({
        title:'登录成功',
        icon:'success',
      })
      setToken(res.data.token)
      setRefreshToken(res.data.refresh_token)
      wx.switchTab({
        url: '/pages/profileuser/profileuser',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})